<?php
/*
 * Created on 14.04.2007
 * @filesource: admin.extensionicons.php
 * @author: Nikolai Plath
 */
$AppVersion = "Extension Icons 0.2m";

defined( '_VALID_MOS' ) or die( 'Direct Access to this script is not allowed');

global $mosConfig_live_site, $mosConfig_absolute_path, $mosConfig_lang;

if ( file_exists( $mosConfig_absolute_path .'/administrator/components/com_extensionicons/language/'.$mosConfig_lang.'.php' ) )
{
	require_once( $mosConfig_absolute_path .'/administrator/components/com_extensionicons/language/'.$mosConfig_lang.'.php' );
} else {
	require_once( $mosConfig_absolute_path .'/administrator/components/com_extensionicons/language/english.php' );
}

require_once( $mainframe->getPath( 'class' ) );
require_once( $mainframe->getPath( 'admin_html' ) );
$id = mosGetParam( $_REQUEST, 'cid', array(0) );

if ( !is_array( $id ))
{
      $id = array(0);
}

if ( ( !$act || $act == 'all' ) && !$task )
{
   $task = "listItems";
}

switch ( $task )
{
   	case "exiconRefresh":
   	  	synchronizeme ( $option , $AppVersion);
   	  	break;
   	case "ConfigModule":
   		ConfigModule ( $option );
   		break;
    case "listItems":
      	listItems( $option , $AppVersion );
      	break;
    case "edit":
      	edit( $option , $id[0], $AppVersion  );
      	break;
    case "new":
    	edit( $option , '', $AppVersion );
    	break;
    case "save":
     	save( $option );
     	break;
    case "delete":
    	delete( $option , $id );
    	break;
    case "publish":
     	publish( $option , '1' , $id );
     	break;
    case "unpublish":
     	publish( $option , '0' , $id );
     	break;
    case 'orderup':
		orderExIcons( intval( $id[0] ), -1, $option );
		break;
	case 'orderdown':
		orderExIcons( intval( $id[0] ), 1, $option );
		break;
	case "MediaManager":
		//HTML_ExtensionIcons::WrapMediaManager();
		mosRedirect( "index2.php?option=com_media&amp;listdir=/ExtensionIcons_custom" );

		break;
	case 'help':
		HelpMe( $option , $AppVersion );
		break;
}//switch

function listItems( $option , $AppVersion )
{
global $database, $mainframe, $mosConfig_list_limit;

    $limit 		= intval( $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', $mosConfig_list_limit ) );
	$limitstart = intval( $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 ) );


    $database->setQuery("SELECT COUNT(*) FROM #__extension_icons");
    $total = $database->loadResult();

	if ( $total == 0 ) echo _EXICONS_NO_EXICONS;

 	require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit  );

     $database->setQuery("SELECT * FROM #__extension_icons"
                         ." ORDER BY ordering", $pageNav->limitstart, $pageNav->limit );
     $rows = $database->loadObjectList();

     HTML_ExtensionIcons::listItems($option, $rows, $pageNav);
     HTML_ExtensionIcons::printFooter( $AppVersion );
}

function edit($option, $uid , $AppVersion )
{
global $database;
     $row = new mosExtensionIcons( $database );
     $row->load($uid);
     HTML_ExtensionIcons::edit( $option, $row );
     HTML_ExtensionIcons::printFooter( $AppVersion );
}

function save( $option )
{
global $database;
     $row = new mosExtensionIcons ( $database );
     $row->bind( $_POST );
     $row->store();
     mosRedirect( "index2.php?option=$option", _EXICONS_SAVED );
}

function delete( $option, $cid )
{
global $database;
     $cids = implode( ',', $cid );
     $database->setQuery( "DELETE FROM #__extension_icons "
                         ."WHERE id IN ($cids)" );
     $database->query();
     mosRedirect( "index2.php?option=$option" );
}

function publish( $option, $publish ,$cid )
{
global $database;
     $cids = implode( ',', $cid );
     $database->setQuery( "UPDATE #__extension_icons "
                         ."SET published='$publish' "
                         ."WHERE id IN ($cids)" );
     $database->query();
     mosRedirect( "index2.php?option=$option" );
}

function orderExIcons( $uid, $inc, $option )
{
global $database;
	$row = new mosExtensionIcons( $database );
	$row->load( (int)$uid );
	$row->updateOrder();
	$row->move( $inc, "published >= 0" );
	$row->updateOrder();

	mosRedirect( "index2.php?option=$option" );
}

function HelpMe( $option , $AppVersion )
{
  HTML_ExtensionIcons::help ( $option , $AppVersion );
}

function ConfigModule ( $option )
{
global $database;
    $database->setQuery("SELECT id FROM #__modules"
                    ."\n WHERE module='mod_extensionicons'");
 	$id = $database->loadResult();
	mosRedirect("index2.php?option=com_modules&client=admin&task=editA&hidemainmenu=1&id=".$id);
}

function synchronizeme ( $option , $AppVersion)
{
global $database;
     echo _EXICONS_SYNC_START . ' ';
     $database->setQuery("SELECT * FROM #__extension_icons"
                         ." ORDER BY id");
     $backendicons = $database->loadObjectList();

     $database->setQuery("SELECT * FROM #__components"
     					." WHERE admin_menu_link != ''"
                         ." ORDER BY id");
    $db_components = $database->loadObjectList();
	$cnt_new = 0;
     foreach ($db_components as $db_component)
     {
     	$found = false;
     	if ( count($db_components > 0))
     	{
	     	foreach ($backendicons as $backendicon)
   		  	{
				if ( $backendicon->admin_menu_link == $db_component->admin_menu_link )
				{
					$found = true;
				}
     		}
    	 }
    	 if ($found == false)
    	 {
    	 	$parent_name = '';
    	 	if ($db_component->parent > 0)
    	 	{
    		 	$query = "SELECT name FROM #__components WHERE id = " . $db_component->parent;
   		 	 	$database->setQuery($query);
   		 	 	$parent_name = $database->loadResult();
    		 }
			$query = "INSERT INTO #__extension_icons"
			."\n SET name = '$db_component->name',"
			."\n parent = $db_component->parent,"
			."\n admin_menu_link = '$db_component->admin_menu_link',"
			."\n admin_menu_alt = '$db_component->admin_menu_alt',"
			."\n `linkoption` = '$db_component->option',"
			."\n admin_menu_img = '$db_component->admin_menu_img'";
			if ($parent_name != '')
			{
				$query .= ",\n parent_name = '$parent_name'";
			}
   		 	$database->setQuery($query);
   		    $database->query();
			$cnt_new += 1;
    	 }//if
     }//foreach
	echo $cnt_new . ' ' . _EXICONS_SYNC_END . ' ' . '<img src="images/tick.png" />';
	listItems ( $option , $AppVersion );
}//function

?>
